local Log = require("common.Log")
local LogDefine = require("common.LogDefine")
local Lang = require("common.Lang")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local GridLogic = require("bag.GridLogic")
local BagLogic = require("bag.BagLogic")
local ItemDefine = require("bag.ItemDefine")
local EquipExcel = require("excel.Equip").equip
local Equip = require("bag.Equip")
local Status = require("scene.Status")
local Grid = require("bag.Grid")
local RefineHandle = require("refine.HandlerL")
local YYDaBiao = require("activity.operate.DaBiao")
local AcvityDefine = require("activity.Define")

-- 装备查询 op
EQUIP_QUERY = 1 -- 查询
EQUIP_CHANGE = 2 -- 改变

OK = 1					-- 查询OK
HUMAN_UUID_INVALID = 2	-- 目标uuid非法
function CG_EQUIP_QUERY(human,uuid,op)
	local msgRet = Msg.gc.GC_EQUIP_LIST
	msgRet.op = op or 1
	msgRet.itemDatas[0] = 0
	
	local target = ObjHuman.onlineUuid[uuid]
  if not target then
      return
  end
	
	msgRet.name = target.name
	msgRet.uuid = target._id
	msgRet.result = OK
	Grid.makeList(human, human.db.equip, 13, msgRet)
	--GridLogic.makeGrids(target.db.equip,equipIndex, 1, 13, msgRet.itemDatas,human)
--	Msg.send(msgRet, human.fd)	
end

function GC_EQUIP_CHANGE(human,index)
	local msgRet = Msg.gc.GC_EQUIP_CHANGE
	GridLogic.makeGrid(human.db.equip[index],msgRet.itemData,index,human)
	Msg.send(msgRet, human.fd)	
	YYDaBiao.checkCanGet(human, AcvityDefine.OPERATEACT_TYPE_EQUIPPOWER)
end

function CG_EQUIP_PUT_ON(human,index)
	local grid = BagLogic.getGridByIndex(human,index)
	local msgRet = Msg.gc.GC_EQUIP_PUT_ON
	msgRet.index = index
	
	local msgErr = Msg.gc.GC_BROADCAST
	msgErr.type = Lang.MOUSE
	
	if grid.id == 0 then
		msgErr.msg = Lang.EQUIP_INDEX_INVALID
		Msg.send(msgErr, human.fd)	
		return 
	end
	
	if not ItemDefine.isEquip(grid.id) then
		msgErr.msg = Lang.EQUIP_NOT_EQUIP
		Msg.send(msgErr, human.fd)
		return 
	end
	
	local conf = EquipExcel[grid.id]
	
	if conf.dressType ~= 1 then
		msgErr.msg = Lang.EQUIP_IS_PET_EQUIP
		Msg.send(msgErr, human.fd)
		return
	end
	if conf.sex ~= 0 and human.db.sex ~= conf.sex then
		msgErr.msg = Lang.EQUIP_SEX_NOT_FIT
		Msg.send(msgErr, human.fd)
		return 
	end
	local needLv = Equip.getPutOnLv(grid)
	if conf.lv ~= 0 and human.db.lv < needLv then
		msgErr.msg = Lang.EQUIP_LEVEL_NOT_ENOUGH
		Msg.send(msgErr, human.fd)
		return 
	end
	if grid.lifeTime ~= 0 and os.time() > grid.lifeTime then
		BagLogic.delGridByIndex(human,index,"grid_over_time")
--		Broadcast.sendBroadcast(human,Lang.MOUSE+Lang.CHAT,string.format(Lang.BAG_GRID_OVER_TIME,HtmlUtil.fontItem(grid)))	
		return 
	end
	local equipNow = human.db.equip[conf.pos-1] 
	local tmp = {}
	Grid.copy(tmp, equipNow, true)
	for i=0, equipNow.data.equip.hole - 1 do
    equipNow.data.equip.gems[i] = 0
  end
	Grid.copy(equipNow, human.db.bag[index], true)
  Grid.copy(human.db.bag[index], tmp)
	local equipGrid = human.db.equip[conf.pos - 1]
	-- set bind 0:绑定；1：不绑
	if conf.putOnBind == 0 then
		equipGrid.bind = 1
	end
	if equipGrid.lifeTime == 0 and ItemDefine.getValue(equipGrid.id,"lifeTime") and ItemDefine.getValue(equipGrid.id,"lifeTime") > 0 then
		equipGrid.lifeTime = ItemDefine.getValue(equipGrid.id,"lifeTime") + os.time()
  end
	equipGrid.data.equip.power = Equip.getZhandouli(human, equipGrid)
	ObjHuman.doCalc(human)
	
	BagLogic.sendBagItemChange(human,index,ItemDefine.BAG_EQUIP)
	
	GridLogic.makeGrid(equipGrid,msgRet.itemData,conf.pos - 1,human)
	Msg.send(msgRet, human.fd)	
	Status.sendViewStatus(human, true)	
	if conf.pos == ItemDefine.WEAPON or conf.pos == ItemDefine.CLOTH
	   or conf.pos == ItemDefine.HORSE then
--		RoleHandler.CG_BODY_INFO(human)
	end
	RefineHandle.CG_OPEN_REFINE_WINDOW(human)
	YYDaBiao.checkCanGet(human, AcvityDefine.OPERATEACT_TYPE_EQUIPPOWER)
end

function CG_EQUIP_PUT_OFF(human,index,bagIndex)
	local msgRet = Msg.gc.GC_EQUIP_PUT_OFF
	msgRet.index = 0
	
	local msgErr = Msg.gc.GC_BROADCAST
	msgErr.type = Lang.MOUSE
	
	if not checkIndex(index) then
		msgErr.msg = Lang.EQUIP_INDEX_INVALID
		Msg.send(msgErr, human.fd)
		return 
	end
	
	if not BagLogic.checkIndex(human,bagIndex) then
		msgErr.msg = Lang.EQUIP_INDEX_INVALID
		Msg.send(msgErr, human.fd)
		return 
	end
	
	local grid = human.db.equip[index]
	if grid.id == 0 then
		msgErr.msg = Lang.EQUIP_NOT_FIND_EQUIP
		Msg.send(msgErr, human.fd)
		return 
	end
	
	if grid.lifeTime ~= 0 and os.time() > grid.lifeTime then
		local conf = EquipExcel[grid.id]	
		
		human.db.equip[index].id = 0
		GC_EQUIP_CHANGE(human,index)
		Status.sendViewStatus(human, true)
		if conf.pos == ItemDefine.WEAPON or conf.pos == ItemDefine.CLOTH then
	--		RoleHandler.CG_BODY_INFO(human)
		end
	--	Broadcast.sendBroadcast(human,Lang.CHAT+Lang.MOUSE,string.format(Lang.EQUIP_GRID_OVER_TIME,HtmlUtil.fontItem(grid)))
		ObjHuman.doCalc(human)
		return	
	end 
	
	local conf = EquipExcel[grid.id]
	local bagGrid = BagLogic.getGridByIndex(human,bagIndex)
	if bagGrid.id ~= 0 then
		msgErr.msg = Lang.EQUIP_TARGET_GRID_NOT_EMPTY
		Msg.send(msgErr, human.fd)
		return 
	end
	
  Grid.copy(human.db.bag[bagIndex], human.db.equip[conf.pos-1],  true)
	
	BagLogic.sendBagItemChange(human,bagIndex,ItemDefine.BAG_EQUIP)
	
	msgRet.index = conf.pos-1
	Msg.send(msgRet, human.fd)	
		
	human.db.equip[index].id = 0
	GC_EQUIP_CHANGE(human,index)
	ObjHuman.doCalc(human)
	Status.sendViewStatus(human, true) 
	--[[
	Status.sendViewStatus(human, true)	
	if conf.pos == ItemDefine.WEAPON or conf.pos == ItemDefine.CLOTH
		or conf.pos == ItemDefine.HORSE then
		RoleHandler.CG_BODY_INFO(human)
	end
	
	EquipCallSkill.update(human)
  --]]
  RefineHandle.CG_OPEN_REFINE_WINDOW(human)
end

function checkIndex(index)
	if not index then
		return false
	end
	if index < 0 or index >=13 then
		return false
	end
	return true
end

-- 根据下标删除一个数量
function delEquipByIndex(human, index, logType, noSend)
	if not LogDefine.DEFINE[logType] then
		assert()
	end
	
	if not checkIndex(index) then
		return false
	end
	
	local grid = human.db.equip[index]
	if not grid then
		assert(grid)
	end
	
	local id = grid.id
	human.db.equip[index] = nil
	
	if not noSend then
		GC_EQUIP_CHANGE(human,index)
	end
	
	local conf = EquipExcel[id]
	if conf.pos == ItemDefine.WEAPON or conf.pos == ItemDefine.CLOTH
		or conf.pos == ItemDefine.HORSE then
		Status.sendViewStatus(human, true)
		RoleHandler.CG_BODY_INFO(human)
	end
	
	Log.writeItemLog(human, grid, -grid.cnt, LogDefine.DEFINE[logType] + LogDefine.TYPE["item"])
end

function getZhandouli(human)
	local zhandouli = 0
	for key,grid in pairs(human.db.equip) do 
		if key ~= 0 then
			zhandouli = zhandouli + Equip.getZhandouli(human,grid)
		end
	end
	return math.floor(zhandouli)
end

function getStrengthLvCount(human, lv)
  local count = 0
  for i = 0, human.db.equipLen-1 do
    local grid = human.db.equip[i]
    if grid ~= nil and grid.id ~=0 then
      if grid.data.equip.strengthLv > lv then
          count = count + 1
      end
    end
  end
  return count
end

function getEquipLvCount(human, lv)
  local count = 0
  for i = 0, human.db.equipLen-1 do
    local grid = human.db.equip[i]
    if grid ~= nil and grid.id ~=0 then
      if EquipExcel[grid.id].lv > lv then
          count = count + 1
      end
    end
  end
  return count
end

function getQualityLvCount(human, lv)
  local count = 0
  for i = 0, human.db.equipLen-1 do
    local grid = human.db.equip[i]
    if grid ~= nil and grid.id ~=0 then
      if grid.data.equip.quality > lv then
          count = count + 1
      end
    end
  end
  return count
end
